package yf.mall.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import java.util.Map;
import com.yf.consts.DaoConst;
import yf.mall.entity.MallTypeEntity;
import yf.mall.enums.MallTypeTypeEnum;
import yf.mall.service.MallTypeService;
import com.yf.response.R;


/**
 * 分类Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Fri Nov 01 10:57:07 CST 2019
 */
@RestController
@RequestMapping("/mall/type")
public class MallTypeController {
    @Autowired
    private MallTypeService mallTypeService;

    /**
     * 列表分类
     */
    @RequestMapping("/list")
    @RequiresPermissions("mall:type:list")
    public R list(@RequestParam Map<String,Object> params) {
        PageHelper.startPage(DaoConst.getPage(params),DaoConst.getLIMIT(params));
        params.put("type", MallTypeTypeEnum.ONE.getIndex());//找要一级的
        List<MallTypeEntity> mallTypeList = mallTypeService.queryList(params);
        //封装children
        mallTypeList.forEach(mallTypeEntity -> {
            List<MallTypeEntity> list = mallTypeService.findByParentId(mallTypeEntity.getId());
            mallTypeEntity.setChildren(list);
        });
        return R.ok(new PageInfo(mallTypeList));
    }

    /**
     * 查看分类
     */
    @SysLog("查看分类")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("mall:type:info")
    public R info(@PathVariable("id") Long id) {
        MallTypeEntity mallType = mallTypeService.get(id);

        return R.ok(mallType);
    }

    /**
     * 保存分类
     */
    @SysLog("保存分类")
    @RequestMapping("/save")
    @RequiresPermissions("mall:type:save")
    public R save(MallTypeEntity mallType) {
        mallTypeService.save(mallType);

        return R.ok(mallType);
    }

    /**
     * 修改分类
     */
    @SysLog("修改分类")
    @RequestMapping("/update")
    @RequiresPermissions("mall:type:update")
    public R update(MallTypeEntity mallType) {
        mallTypeService.update(mallType);

        return R.ok(mallType);
    }

    /**
     * 删除分类
     */
    @SysLog("删除分类")
    @RequestMapping("/delete")
    @RequiresPermissions("mall:type:delete")
    public R delete(Long[]ids) {
        mallTypeService.deleteByTypeIdBatch(ids);
        mallTypeService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 所有分类
     */
    @SysLog("所有分类")
    @RequestMapping("/queryAll")
    public R queryAll (@RequestParam Map<String,Object> params) {

        List<MallTypeEntity> mallTypeList = mallTypeService.queryList(params);
        //封装children
        mallTypeList.forEach(mallTypeEntity -> {
            List<MallTypeEntity> list = mallTypeService.findByParentId(mallTypeEntity.getId());
            mallTypeEntity.setChildren(list);
        });
            return R.ok(mallTypeList);
    }

    /**
    @ModelAttribute
    public SearchParam<MallTypeEntity> searchParam(SearchParam<MallTypeEntity> params, MallTypeEntity mallType) {
        if (mallType != null) {
            params.setEntity(mallType);
        }

        return params;
    }
 **/
    /***********自定义开始***********/
}
